package system

import (
	"server/global"
	
	"server/model/system"
	systemReq "server/model/system/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: Register
//@description: 用户注册
//@param: u model.SysArts
//@return: userInter system.SysArts, err error

type InfoService struct{}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserInfoList
//@description: 分页获取数据
//@param: info request.PageInfo
//@return: err error, list interface{}, total int64

func (infoService *InfoService) GetArtsInfoList(info systemReq.SysInfoSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&system.SysArts{})
	if info.Title != "" {
		db = db.Where("title like ?", "%"+info.Title+"%")
	}
	if info.Desc != "" {
		db = db.Where("`desc` like ?", "%"+info.Desc+"%")
	}
	if info.Content != "" {
		db = db.Where("content like ?", "%"+info.Content+"%")
	}
	
	var userList []system.SysArts
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&userList).Error
	return userList, total, err
}

func (infoService *InfoService) SetArtsInfo(req system.SysArts) error {
	return global.GVA_DB.Updates(&req).Error
}

func (infoService *InfoService) DeleteArt(id int) (err error) {
	var user system.SysArts
	err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error

	return err

}

func (infoService *InfoService) CreateArtInfo(u system.SysArts) (userInter system.SysUser, err error) {

	err = global.GVA_DB.Create(&u).Error

	return userInter, err
}
